<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Model\Admin\Admin;
use App\Model\ChargeOption;
use App\Model\Device;
use App\Model\Finance;
use App\Model\RefundLog;
use App\Model\Station;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use RedisException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use App\Model\Distributors;

class RefundController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected ChargeOption $chargeOption;

    #[Inject]
    protected Distributors $distributors;

    #[Inject]
    protected Device $device;

    #[Inject]
    protected Admin $admin;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**
     * 列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request): ResponseInterface
    {
        try {
            $info = $this->admin->getInfo($request);
            if (!$info) {
                return $this->error(code: '400', msg: '登录态失效 请重新登录');
            }
            $page = $request->inputs(['page', 'limit', 'refund_no', 'status', 'type']);
            $model = RefundLog::query();
            $data['total'] = $model->count();
            $data['list'] = $model
                ->where('refund_no', $page['refund_no'])
                ->where(function ($query) use ($page) {
                    if (is_string($page['status'])) {
                        $query->where('status', $page['status']);
                    }
                })
                ->where(function ($query) use ($page) {
                    if (is_string($page['type'])) {
                        $query->where('type', $page['type']);
                    }
                })
                ->orderByDesc('id')
                ->offset(($page['page'] - 1) * $page['limit'])
                ->limit($page['limit'])
                ->get();
            return $this->success($data);
        } catch (\Exception $e) {
            return $this->error(1001, $e->getMessage() . $e->getTraceAsString());
        }
    }
}
